//
//  CommentViewController.m
//  口袋
//
//  Created by dllo on 15/5/27.
//  Copyright (c) 2015年 口袋. All rights reserved.
//

#import "CommentViewController.h"
#import "CommentCell.h"
#import "CommentModel.h"
#import "SANetWorkingTask.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "DKNightVersion.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@interface CommentViewController ()<UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>

@end

@implementation CommentViewController
- (void)dealloc
{
    [_table release];
    [_docID release];
    [_type release];
    [_array release];
    
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
        //self.coldArray = [NSMutableArray array];
    }
    return self;
}

- (void)loadView
{
    [super loadView];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];
    //热门
    NSString *hotUrl = [NSString stringWithFormat:@"http://comment.api.163.com/api/json/post/list/new/hot/%@/%@/0/10/10/2/2", self.type, self.docID];
    NSLog(@"url = %@", hotUrl);
    [SANetWorkingTask request:hotUrl parmater:nil block:^(NSDictionary *result) {
        //NSMutableArray *arr = [result objectForKey:@"newPosts"];
        //[arr arrayByAddingObjectsFromArray:[result objectForKey:@"newPosts"]];
        if ([result objectForKey:@"hotPosts"] != [NSNull null]) {
            NSArray *array = [result objectForKey:@"hotPosts"];
            NSMutableArray *smallArray = [NSMutableArray array];
            
            NSMutableDictionary *hotDic = [NSMutableDictionary dictionary];
            for (NSDictionary *dic in array) {
                NSDictionary *smallDic = [dic objectForKey:@"1"];
                
                CommentModel *model = [[CommentModel alloc]init];
                [model setValuesForKeysWithDictionary:smallDic];
                [smallArray addObject:model];
                
            }
            [hotDic setObject:smallArray forKey:@"array"];
            [hotDic setObject:@"热门评论" forKey:@"title"];
            [self.array addObject:hotDic];
            [self.table reloadData];
            [self createTable];
            
            NSString *coldUrl = [NSString stringWithFormat:@"http://comment.api.163.com/api/json/post/list/new/normal/%@/%@/desc/0/10/10/2/2", self.type, self.docID];
            NSLog(@"cold = %@", coldUrl);
            [SANetWorkingTask request:coldUrl parmater:nil block:^(NSDictionary *result) {
                
                
                NSMutableArray *smallArray = [NSMutableArray array];
                NSMutableDictionary *coldDic = [NSMutableDictionary dictionary];
                NSArray *array = [result objectForKey:@"newPosts"];
                for (NSDictionary *dic in array) {
                    NSDictionary *smallDic = [dic objectForKey:@"1"];
                    CommentModel *model = [[CommentModel alloc]init];
                    [model setValuesForKeysWithDictionary:smallDic];
                    [smallArray addObject:model];
                    [model release];
                    
                }
                [coldDic setObject:smallArray forKey:@"array"];
                [coldDic setObject:@"普通评论" forKey:@"title"];
                [self.array addObject:coldDic];
                [self.table reloadData];
                [self createTable];
                
            }];
            
        } else {
            
            UIAlertView *tips = [[UIAlertView alloc]initWithTitle:@"提示" message:@"还没有评论" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
            tips.delegate = self;
            [tips show];
        }

    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:118/255.0 green:210/255.0 blue:245/255.0 alpha:1.0]];
    self.navigationController.navigationBar.nightBarTintColor = UIColorFromRGB(0x343434);
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)createTable
{
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - - 64 - 49) style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.dataSource = self;
    //self.table.rowHeight = 150;
    [self.view addSubview:self.table];
    [self.table release];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.array.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSDictionary * dic = [self.array objectAtIndex:section];
    
    if ([[dic objectForKey:@"title"] isEqualToString:@"热门评论"]) {
        NSArray *hotArray = [dic objectForKey:@"array"];
        return hotArray.count;
    }else{
        NSArray *coldArray = [dic objectForKey:@"array"];
        return coldArray.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [self.array objectAtIndex:indexPath.section];
    NSArray *array = [dic objectForKey:@"array"];
    CommentModel *model = [array objectAtIndex:indexPath.row];
    
    CommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[CommentCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    cell.headIcon.layer.cornerRadius = 21;
    cell.headIcon.layer.masksToBounds = YES;
    
    NSString *picurl = model.timg;
    if (picurl) {
        [cell.headIcon sd_setImageWithURL:[NSURL URLWithString:model.timg] placeholderImage:[UIImage imageNamed:@"loading.jpg"]];
    }else{
        [cell.headIcon setImage:[UIImage imageNamed:@"默认头像.jpg"]];
    }
    cell.noteLabel.text = model.b;
    NSString *str = cell.noteLabel.text;
    //给label设置高度
    NSDictionary *dicc = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    CGRect rect = [str boundingRectWithSize:CGSizeMake(250, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dicc context:nil];
    [cell.noteLabel setFrame:CGRectMake(cell.phoneLabel.frame.origin.x, 0.15 * WIDTH, 0.8 *WIDTH, rect.size.height)];
   // cell.noteLabel.text = model.b;
    cell.noteLabel.numberOfLines = 0;
    
    NSString *name = model.n;
    if (name) {
        cell.nameLabel.text = name;
        NSArray *nameArray = [model.f componentsSeparatedByString:@"&"];
        cell.phoneLabel.text = [nameArray firstObject];
        
    }else{
        cell.nameLabel.text = model.f;
        cell.phoneLabel.text = model.f;
        
    }
    
    return cell;
    
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    NSDictionary *dic = [self.array objectAtIndex:section];
    NSString *title = [dic objectForKey:@"title"];
    return title;
}

//给cell设置高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [self.array objectAtIndex:indexPath.section];
    NSArray *array = [dic objectForKey:@"array"];
    CommentModel *model = [array objectAtIndex:indexPath.row];
    NSString *str = model.b;
    NSDictionary *dicc = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
    CGRect rect = [str boundingRectWithSize:CGSizeMake(250, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dicc context:nil];
    return rect.size.height + 70;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
